package com.xhtt.modules.app.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xhtt.common.utils.Constant;
import com.xhtt.common.utils.R;
import com.xhtt.modules.app.annotation.Login;
import com.xhtt.modules.app.annotation.LoginUser;
import com.xhtt.modules.app.form.LoginForm;
import com.xhtt.modules.app.utils.JwtUtils;
import com.xhtt.modules.sys.entity.SysUserAppLog;
import com.xhtt.modules.sys.entity.SysUserEntity;
import com.xhtt.modules.sys.service.SysUserAppLogService;
import com.xhtt.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.crypto.hash.Sha256Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * APP登录授权
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2017-03-23 15:31
 */
@RestController
@RequestMapping("app")
@Api(value = "登录接口", tags = "登录接口")
public class AppLoginController {
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private SysUserAppLogService appLogService;

    /**
     * 登录
     */
    @PostMapping("/login")
    @ApiOperation("登录(web/app)")
    public R login(@RequestBody LoginForm form) {
        //用户信息
        SysUserEntity user = sysUserService.getOne(new QueryWrapper<SysUserEntity>().eq(SysUserEntity.USERNAME, form.getUsername()).or().eq(SysUserEntity.WORKNO, form.getUsername()));

        //账号不存在、密码错误
        if (user == null || !user.getPassword().equals(new Sha256Hash(form.getPassword(), user.getSalt()).toHex())) {
            return R.error("账号或密码不正确");
        }

        //账号锁定
        if (user.getStatus() == 0) {
            return R.error("账号已被锁定,请联系管理员");
        }

        //生成token
        String token = jwtUtils.generateToken(user.getUserId());

        Map<String, Object> map = new HashMap<>();
        map.put("token", token);
        map.put("expire", jwtUtils.getExpire());

        SysUserEntity u = new SysUserEntity();
        u.setUserId(user.getUserId());
        u.setLastLoginDate(LocalDateTime.now());
        u.setLoginStatus(1);
        sysUserService.updateById(u);
        Constant.webMenu.put(user.getUserId(), null);//登陆成功初始化菜单数据的map
        Constant.appMenu.put(user.getUserId(), null);
        //app登陆记日志
        if (StringUtils.isNotEmpty(form.getPadNo())) {
            SysUserAppLog log = new SysUserAppLog(user.getUserId().intValue(), form.getPadNo(), LocalDateTime.now());
            appLogService.save(log);
        }

        return R.ok(map);
    }

    /**
     * 退出
     */
    @Login
    @PostMapping("/logout")
    public R logout(@ApiIgnore @LoginUser SysUserEntity user) {
        SysUserEntity u = new SysUserEntity();
        u.setUserId(user.getUserId());
        u.setLoginStatus(0);
        sysUserService.updateById(u);
        return R.ok();
    }
}
